package com.codeduck.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @date: 2023/2/5
 */
@Component
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext ac;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType) {
        return ac.getBean(requiredType);
    }

    public static <T> T getBean(String beanName) {
        return (T) ac.getBean(beanName);
    }
}
